﻿using UnityEngine;
using System.Collections;

public class Cannon_Script : MonoBehaviour 
{
	//Public Var
	public GameObject shot;			//Fire Prefab
	public GameObject shotUltimate;	//Ultimate Prefab

	public float maxfireRate = 2.0F;//Fire Rate between Shots
	public float minfireRate = 0.5F;//Fire Rate between Shots

	public float timeAtMax = 1.0f;//Time to stay in Max firerate

	//public FollowMousy mouse;
	public GameObject aim;

	//Private Var
	private float nextFire = 0.0f;	//First fire & Next fire Time
	private float fireRate;	//Fire Rate between Shots
	//private float currentTimeAtMax = 0.0f;

	// Update is called once per frame
	void Start () 
	{
		ResetFireRate();
	}

	public void ResetFireRate()
	{
		fireRate = minfireRate;
		//currentTimeAtMax = 0.0f;
	}

//	public void TryToShoot(float fireRateDesacceleration)
//	{
//		//Excute When the Current Time is bigger than the nextFire time
//		if (!HighScore.gameover && Time.time > nextFire && mouse.getState() == FollowMousy.mousyState.Aim) 
//		{
//			if(currentTimeAtMax < timeAtMax)
//			{
//				currentTimeAtMax += Time.deltaTime;
//			}
//			else
//			{
//				fireRate += fireRateDesacceleration;
//			}
//
//			if(fireRate > maxfireRate)
//			{
//				fireRate = maxfireRate;
//			}
//			nextFire = Time.time + fireRate;//Increment nextFire time with the current system time + fireRate
//			Vector3 pos = transform.position;
//
//			Instantiate (shot, pos, transform.rotation); 	//Instantiate fire shot 
//			GetComponent<AudioSource>().Play (); 													//Play Fire sound
//		}
//	}

	public void ShootAtFireRate(float pfireRate, bool ultimate)	{
		//Excute When the Current Time is bigger than the nextFire time
		if (!HighScore.gameover && Time.time > nextFire /*&& mouse.getState() == FollowMousy.mousyState.Aim*/) 
		{
			nextFire = Time.time + pfireRate;//Increment nextFire time with the current system time + fireRate
			if(ultimate)
			{
				Instantiate (shotUltimate, transform.position, transform.rotation); 	//Instantiate fire shot 
			}
			else
			{
				shot.GetComponent<PlayerShipShots_Script>().Aim = aim;
				Instantiate (shot, transform.position, transform.rotation); 	//Instantiate fire shot 
			}
			GetComponent<AudioSource>().Play(); 													//Play Fire sound
		}
	}
	// FixedUpdate is called one per specific time
	void FixedUpdate ()
	{

	}
}
